#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/9/13 11:26
# @Author  : 王凯
# @File    : shaanxi_illegal.py
# @Project : spider-man

from apps.tax_illegal.tax_illegal.items import NetTaxIllegalProItem
from apps.tax_illegal.tax_illegal.spiders import BaseTaxIllegalSpider


class TianjinIllegalSpider(BaseTaxIllegalSpider):
    name = "tianjin_illegal"
    province: str = "天津"
    url: str = "https://tianjin.chinatax.gov.cn/wzcx/cx_zdwfaj.action?szsf=11200000000"  # 取首页

    def start_requests(self):
        yield from self.etax_search(**{"etax_url": "https://etax.tianjin.chinatax.gov.cn:8443"})
        url = "https://tianjin.chinatax.gov.cn/wzcx/dycx_getQyxx.action"
        for i in []:
            data = {
                "nsrmc": "",
                "nsrsbh": "91",
                "zcdz": "",
                "zzjgdm": "",
                "fddbrxm": "",
                "fddbrsfzh": "",
                "cwfzrxm": "",
                "cwfzrsfzh": "",
                "button2": " 查   询 ",
            }
            yield self.FormRequest(url, formdata=data, callback=self.parse, method="POST")

    def parse(self, response, **kwargs):
        yield from self.parse_detail(response, **kwargs)
        data_list = response.xpath('//span[@class="mxxx"]')
        if len(data_list) == 20:
            print("有下一页")

    def parse_detail(self, response, **kwargs):
        data_list = response.xpath('//span[@class="mxxx"]')
        mapping_dict = {
            "data-nsrsbh": "taxpayer_id",
            "data-nsrmc": "company_name",
            "data-zzjgdm": "org_code",
            "data-zcjydz": "address",
            "data-ajlxmc": "illegal_status",
            "data-zywfss": "illegal_facts",
            "data-clqk": "basis_and_punishment",
        }

        for data in data_list:
            data = data.attrib
            item = NetTaxIllegalProItem()
            item.province = self.province
            for i in mapping_dict.keys():
                setattr(item, mapping_dict.get(i), data.get(i))
            if data.get("data-fddbrxm"):
                item.legal_representative = (
                    f"姓名：{data.get('data-fddbrxm')}、性别：{data.get('data-fddbrxb')}、"
                    f"身份证件名称：{data.get('data-fddbrzjmc')}、身份证件号码：{data.get('data-fddbrzjhm')}"
                )
            if data.get("data-wfssfssfddbrxm"):
                item.illegal_legal_representative = (
                    f"姓名：{data.get('data-wfssfssfddbrxm')}、性别：{data.get('data-wfssfssfddbrxb')}、"
                    f"身份证件名称：{data.get('data-wfssfssfddbrzjmc')}、身份证件号码：{data.get('data-wfssfssfddbrzjhm')}"
                )
            if data.get("data-cwfzrxm"):
                item.resp_financial_head = (
                    f"姓名：{data.get('data-cwfzrxm')}、性别：{data.get('data-cwfzrxb')}、"
                    f"身份证件名称：{data.get('data-cwfzrzjmc')}、身份证件号码：{data.get('data-cwfzrzjhm')}"
                )
            if data.get("data-zjjgxx"):
                item.resp_intermediary = data.get("data-zjjgxx")
            yield item


if __name__ == "__main__":

    from scrapy import cmdline

    cmdline.execute(argv=["scrapy", "crawl", "tianjin_illegal"])
